package com.scst.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.scst.constant.MessageConstant;
import com.scst.constant.StatusConstant;
import com.scst.dto.DishDTO;
import com.scst.dto.DishPageQueryDTO;
import com.scst.entity.Dish;
import com.scst.entity.DishFlavor;
import com.scst.exception.DeletionNotAllowedException;
import com.scst.mapper.DishFlavorMapper;
import com.scst.mapper.DishMapper;
import com.scst.result.PageResult;
import com.scst.service.DishService;
import com.scst.service.SetmealService;
import com.scst.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealService setmealService;

    @Transactional
    @Override
    public void saveWithFlavors(DishDTO dishDTO) {
        //向菜品表插入一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        //向口味表插入n条数据
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        if (dishFlavorList != null && dishFlavorList.size() > 0) {
            dishFlavorMapper.insertBenth(dishFlavorList);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        //判断当前id是否能够被删除
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断当前菜品是否能够被删除--是否被套餐关联了
        List<Long> setmaelIds = setmealService.getSetmealIdsByDishIds(ids);
        if (setmaelIds != null && setmaelIds.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //批量删除菜品
        for (Long id : ids) {
            dishMapper.deteleById(id);
            dishFlavorMapper.deleteById(id);
        }
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        Dish dish=dishMapper .getById(id);
        List<DishFlavor> dishFlavorList=dishFlavorMapper .getByDishId(id);
        DishVO dishVO =new DishVO();
        BeanUtils.copyProperties(dish,dishVO );
        dishVO .setFlavors(dishFlavorList);
        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils .copyProperties(dishDTO ,dish);
        //更新菜品
        dishMapper.update(dish);
        //删除原有的口味，然后插入新的
        dishFlavorMapper .deleteById(dishDTO .getId() );
        List<DishFlavor> flavors=dishDTO.getFlavors();
        if(flavors !=null && flavors.size()>0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishDTO.getId());
            });
        }
        dishFlavorMapper.insertBenth(flavors);
    }

    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
